package com.bedrock.project.inside.api.v1;

import com.bedrock.common.utils.poi.ExcelUtil;
import com.bedrock.framework.aspectj.lang.annotation.Log;
import com.bedrock.framework.aspectj.lang.enums.BusinessType;
import com.bedrock.framework.web.controller.BaseController;
import com.bedrock.framework.web.domain.AjaxResponse;
import com.bedrock.framework.web.domain.AjaxResult;
import com.bedrock.framework.web.page.TableDataInfo;
import com.bedrock.project.inside.entity.WorkHour;
import com.bedrock.project.inside.service.WorkHourService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工时管理
 */
@RestController
@RequestMapping("/workhour/v1")
public class WorkHourController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(WorkHourController.class);

    @Autowired
    private WorkHourService workHourService;



    /**
     * 查询工时数字字典
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:list')")
    @GetMapping("/getHourDictData")
    public AjaxResult getHourDictData() {
        return AjaxResult.success(workHourService.getHourDictData());
    }




    /**
     * 查询工时填报列表
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:list')")
    @GetMapping("/getHourList")
    public TableDataInfo getHourList(WorkHour workHour) {
        List<WorkHour> list = workHourService.selectWorkHourList(workHour);
        return getDataTableByList(list);
    }



    /**
     * 导出工时填报列表
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:export')")
    @Log(title = "工时填报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WorkHour workHour) {
        List<WorkHour> list = workHourService.selectWorkHourList(null);
        ExcelUtil<WorkHour> util = new ExcelUtil<WorkHour>(WorkHour.class);
        return util.exportExcel(list, "hour");
    }



    /**
     * 获取工时填报详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:query')")
    @ApiOperation("获取工时详细信息 -- toby.zhang")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workHourId",value="工时Id",dataType="Long", required = true,paramType = "object",example="100")
    })
    @GetMapping(value = "/{workHourId}")
    public AjaxResponse<Map> getInfo(@PathVariable("workHourId") Long workHourId) {
        return AjaxResponse.success(workHourService.selectWorkHourById(workHourId));
    }



    @GetMapping(value = "/getProjectList")
    public AjaxResult getProjectList() {
        return AjaxResult.success(workHourService.getProjectList());
    }



    /**
     * 新增工时填报
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:add')")
    @Log(title = "工时填报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult insertWorkHour(@RequestBody WorkHour workHour) {
        return workHourService.insertWorkHour(workHour);
    }



    /**
     * 修改工时填报
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:edit')")
    @Log(title = "工时填报", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public AjaxResult update(@RequestBody WorkHour workHour) {
        return toAjax(workHourService.updateWorkHour(workHour));
    }



    /**
     * 删除工时填报
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:remove')")
    @Log(title = "工时填报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workHourIds}")
    public AjaxResult remove(@PathVariable Long[] workHourIds) {
        return toAjax(workHourService.deleteWorkHourByIds(workHourIds));
    }



    /**
     * 工时周报列表
     */
//    @PreAuthorize("@ss.hasPermi('system:workhour:add')")
    @Log(title = "发送工时周报", businessType = BusinessType.INSERT)
    @PostMapping("/sendWeekReport")
    public AjaxResult sendWeekReport() {
        return workHourService.sendWeekReport();
    }

    /**
     * 周报工时确认回写-销售
     * @param userId 销售员Id
     * @param confirmId 确认工时邮件Id
     * @return
     */
    @Log(title = "周报工时确认", businessType = BusinessType.UPDATE)
    @GetMapping("/confirmWorkHour")
    public AjaxResponse confirmWorkHour(Long userId,Long confirmId) {
        return AjaxResponse.success(workHourService.confirmWorkHour(userId,confirmId));
    }

    /**
     * 批量新增工时填报
     * @param workHours 批量工时信息
    */
    @ApiOperation("批量工时填报 -- toby.zhang")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workHours",value="批量工时信息",dataType="List", required = true,paramType = "application/json",example="[{\"params\":{},\"projectId\":24,\"projectType\":\"1\",\"startDate\":1588123424181,\"userId\":1,\"workHourCount\":0.0},{\"params\":{},\"projectId\":24,\"projectType\":\"1\",\"startDate\":1588123424181,\"userId\":1,\"workHourCount\":0.0}]")
    })
    @Log(title = "批量工时填报", businessType = BusinessType.INSERT)
    @PostMapping("/addbatch")
    public AjaxResponse<WorkHour> insertBatchWorkHour(@RequestBody List<WorkHour> workHours) {
        AjaxResponse ajaxResponse = null;
        try {
            workHourService.insertBatchWorkHour(workHours);
            ajaxResponse = AjaxResponse.success();
        } catch (Exception e) {
            logger.error("添加工时异常" + e);
            ajaxResponse = AjaxResponse.error("添加工时失败");
        }
        return ajaxResponse;
    }

    /**
     * 复制上周工时
     */
    @ApiOperation("复制上周工时 -- toby.zhang")
    @GetMapping("/copyLastWeekWorkHour")
    public AjaxResponse<WorkHour> copyLastWeekWorkHour() {
        return AjaxResponse.success(workHourService.copyLstWeekWorkHour());
    }

}


